<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
	<title>码表维护</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" href="../../css/style.css">
	<script type="text/javascript" src="../../js/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript" src="../../js/jquery-1.8.2.js"></script>
	<script type="text/javascript" src="../../js/dialog/jquery.artDialog.source.js?skin=default"></script>
	<script type="text/javascript" src="../../js/dialog/plugins/iframeTools.source.js"></script>
	<script type="text/javascript" src="../../js/jquery.ztree.all-3.5.min.js"></script>
	<script type="text/javascript" src="../../js/jquery.form.js"></script>
	<script type="text/javascript" src="../../js/jquery.page.js"></script>
	<script type="text/javascript" src="../../js/base.js"></script>
	<script type="text/javascript">

	</script>
</head>
<body class="app">
	<h3>码表维护</h3>
	<input type="button" value="添加" id="showadd" class="inputButton fr" style="margin-right:3px;"/>
	<div class="cl"></div>
	<table width="100%" style="margin-top:5px;">
		<tr>
			<td width="180px" valign="top" style="padding-right:5px;">
				<div id="treeRoot" class="inputButton">码表根</div>
				<div id="codeTree" class="ztree"></div>			
			</td>
			<td style=""><div id="list" style="width:99%"></div></td>
		</tr>
	</table>
	
	<div id="dialog" style="display:none;">
	<form class="from" name="addfrom" action="mbwh_save.action" method="post">
		<input type="hidden" name="codeid"/>
		<table style="width:100%">
			<tr>
				<td align="right">上层节点：</td>
				<td>
					<input type="text" name="pidname" class="inputText" value="根节点" style="width:129px" readonly="readonly"/>
					<input type="hidden" name="codepid"/ value="0">
				</td>
			</tr>
			<tr>
				<td align="right">字段：</td>
				<td><input type="text" name="field" class="inputText" style="width:129px"/></td>
			</tr>
			<tr>
				<td align="right">字段名：</td>
				<td><input type="text" name="fieldname" class="inputText" style="width:129px"/></td>
			</tr>
			<tr>
				<td align="right">编码：</td>
				<td><input type="text" name="code" class="inputText" style="width:129px"/></td>
			</tr>
			<tr>
				<td align="right">编码描述：</td>
				<td><input type="text" name="codedesc" class="inputText" style="width:129px"/></td>
			</tr>
			<tr>
				<td align="right">启用：</td>
				<td>
					<select name="enabled" class="inputText">
						<option value="0">禁用</option>
						<option value="1" selected="selected">启用</option>
					</select>
			</tr>
			<tr>
				<td align="right">排序：</td>
				<td><input type="text" name="sortno" class="inputText" style="width:129px" value="0"/></td>
			</tr>
			<tr>
				<td align="right">备注：</td>
				<td><input type="text" name="remark" class="inputText" style="width:129px"/></td>
			</tr>
		</table>
	</form>
	</div>
	<script type="text/javascript">
	$(function(){
		$('#list').page({
			url:'mbwh_typeinfo_list.action',
			rows:10,
			pager:'bottom',
			config:'record,pageinfo,first,last,next,prev,jump',
			items:[{
				name:'codeid',
				type:'checkbox',
				width:13
			},{
				title:'编码ID',
				name:'codeid'
			},{
				title:'代码',
				name:'code'
			},{
				title:'代码描述',
				name:'codedesc'
			},{
				title:'启用',
				name:'enabled',
				render:function(row){
					return row.enabled != 0 ? '启用':'禁用';
				}
			},{
				title:'备注',
				name:'remark'
			},{
				title:'操作',
				width:105,
				render:function(row){
					var $edit = $('<span class="ico edit">编辑</span>');
					var $del = $('<span class="ico del">删除</span>');
					$edit.click(function(){
						dialogOpen(row);
					});
					$del.click(function(){
						$.dialog.confirm('你确定要删除这掉消息吗？', function () {
							del(row);
						});
					});
					return $edit.after($del);
				}
			}]
		});

		$('#showadd').click(function(){
			dialogOpen();
		});
		$(document.addfrom).ajaxForm({
			beforeSubmit:addfrom_before,
			success:addfrom_success
		});
		$('#treeRoot').click(function(){
			var treeObj = $.fn.zTree.getZTreeObj("codeTree");
			treeObj.cancelSelectedNode();
			nodeClick({codeid:0,codedesc:'根节点'});
		});
		$.fn.zTree.init($('#codeTree'),{
			data: {
				key:{
					name:'codedesc'
				},
				simpleData: {
					enable: true,
					idKey:'codeid',
					pIdKey:'codepid',
					rootPId:0
				}
			},
			async: {
				enable: true,
				url:"mbwh_typeinfo_tree.action",
				autoParam:["id=codeid","name=codedesc"]
			},
			callback:{
				onClick:function(e,id,node){
					nodeClick(node);
				}
			}
		});
	});

	var d = $.dialog({
		id:'savedialog',
		title:'代码设置',
		show:false,
		top:'10%',
		left:'35%',
		close: function () {
	        this.hide();
	        return false;
	    },
		button:[{
			name:'保存',
			callback:function(){
				this.content('<span>loading..</span>');
				$(document.addfrom).submit();
			}
		}]
	});
	var dialogOpen = function(node){
		var treeObj = $.fn.zTree.getZTreeObj("codeTree");
		if(typeof(node) == 'object'){
			//编辑
			if(node.codepid > 0){
				var nodes = treeObj.getNodeByParam('codeid',node.codepid,null);
				$(document.addfrom.pidname).val(nodes.codedesc);
			}else{
				$(document.addfrom.pidname).val($('#treeRoot').text());
			}
			$(document.addfrom.codeid).val(node.codeid);
			$(document.addfrom.codepid).val(node.codepid);
			$(document.addfrom.field).val(node.field);
			$(document.addfrom.fieldname).val(node.fieldname);
			$(document.addfrom.code).val(node.code);
			$(document.addfrom.codedesc).val(node.codedesc);
			$(document.addfrom.enabled).val(node.enabled);
			$(document.addfrom.sortno).val(node.sortno);
			$(document.addfrom.remark).val(node.remark);
		}else{
			//初始化
			$(document.addfrom).resetForm();
			$(document.addfrom.codeid).val('');
			var nodes = treeObj.getSelectedNodes();
			if(nodes.length > 0){
				$(document.addfrom.pidname).val(nodes[0].codedesc);
				$(document.addfrom.codepid).val(nodes[0].codeid);
				$(document.addfrom.field).val(nodes[0].field);
				$(document.addfrom.fieldname).val(nodes[0].fieldname);
			}
		}
		d.content(document.getElementById('dialog'));
		d.show()
	}
	
	var nodeClick = function(node){
		$(document.addfrom.pidname).val(node.codedesc);
		$(document.addfrom.codepid).val(node.codeid);
		$(document.addfrom.field).val(node.field);
		$(document.addfrom.fieldname).val(node.fieldname);
		$('#list').page('param',{codepid:node.codeid});
		$('#list').page('show',1);
	}
	
	var addfrom_before = function(param){
		return true;
	}
	var addfrom_success = function(response){
		var treeObj = $.fn.zTree.getZTreeObj("codeTree");
		treeObj.reAsyncChildNodes(null, "refresh");
		$('#list').page('show');
		d.hide();
	}
	var del = function(row){
		var treeObj = $.fn.zTree.getZTreeObj("codeTree");
		$.post('mbwh_del.action',row,function(response){
			treeObj.reAsyncChildNodes(null, "refresh");
			$('#list').page('show');
		});
	}	
	</script>
</body>
</html>
